package com.gm.wholesale.module.good.ui.goodManger;

import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.util.viewUtils.KeyboardUtils;
import com.gm.commonlib.views.choiceview.CustomItemChosenButton;
import com.gm.commonlib.views.dialog.DialogUtil;
import com.gm.commonlib.views.dialog.InputDialog;
import com.gm.commonlib.views.dialog.loadingDialog.LoadingDialog;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EvenBusUtil;
import com.gm.wholesale.module.good.GoodType;
import com.gm.wholesale.module.good.bean.CreateGoodReq;
import com.gm.wholesale.module.good.bean.GoodDetailBean;
import com.gm.wholesale.module.good.bean.GoodListBean;
import com.gm.wholesale.module.good.model.GoodModel;
import com.gm.wholesale.module.good.view.CreateInventoryGoodView;
import com.gm.wholesale.module.good.view.UnInventoryGoodView;
import com.gm.wholesale.module.kind.bean.KindBean;
import com.gm.wholesale.module.kind.model.KindModel;
import com.gm.wholesale.module.unit.bean.UnitBean;
import com.gm.wholesale.module.unit.model.UnitModel;
import com.gm.wholesale.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

public class CreateGoodFragment extends BaseFragment implements View.OnClickListener {
    private SuperTextView cancleBtn;
    private SuperTextView subMitBtn;
    private EditText goodNameTv;
    private TextView titleTv;
    private RadioGroup mRadioGroup;

    private List<KindBean> kindBeans = new ArrayList<>();


    private List<UnitBean> unitBeans;

    public boolean mIsInventoryGood = true;
    public KindBean mChoiceKind;

    private String mGoodName;
    private CreateInventoryGoodView createInventoryGoodView;
    private UnInventoryGoodView unInventoryGoodView;
    private GoodListBean goodListBean;
    private GoodDetailBean goodDetailBean;

    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        getKinds(false, null);
        getUnits(null,false);
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_create_good, parent);
        initView(view);
        if (getArguments() != null && getArguments().getSerializable("data") != null) {
            goodListBean = (GoodListBean) getArguments().getSerializable("data");
            titleTv.setText("编辑商品");
        }
        if (goodListBean != null) {
            getGoodDetail();
        }
    }

    private void getGoodDetail() {
        GoodModel.getGoodDetail(null, goodListBean.getId(), new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(DataResp<GoodDetailBean> resp) {
                if (resp != null && resp.getData() != null) {
                    goodDetailBean = resp.getData();
                    mChoiceKind = new KindBean();
                  GoodDetailBean.CategoryBean categoryBean= goodDetailBean.getCategory();
                  if (categoryBean!=null){
                      mChoiceKind.setName(categoryBean.getPrimaryName());
                      mChoiceKind.setId(categoryBean.getId());
                      goodDetailBean.setCategory(categoryBean);
                  }
                    goodNameTv.setText(goodDetailBean.getName());
                    if (resp.getData().getType()==GoodType.INVENTORY_GOOD) {
                        mRadioGroup.check(R.id.inventoryGood);
                        mIsInventoryGood = true;
                        unInventoryGoodView.setVisibility(View.GONE);
                        createInventoryGoodView.setVisibility(View.VISIBLE);
                        createInventoryGoodView.showView(goodDetailBean);
                    } else {
                        mRadioGroup.check(R.id.unInventoryGood);
                        mIsInventoryGood = false;
                        unInventoryGoodView.setVisibility(View.VISIBLE);
                        createInventoryGoodView.setVisibility(View.GONE);
                        unInventoryGoodView.showView(goodDetailBean);
                    }
                }
            }
        });
    }


    private void initView(View view) {
        mRadioGroup = view.findViewById(R.id.radioGroup);
        cancleBtn = view.findViewById(R.id.cancleBtn);
        subMitBtn = view.findViewById(R.id.subMitBtn);
        goodNameTv = view.findViewById(R.id.goodNameTv);
        titleTv=view.findViewById(R.id.titleTv);
        cancleBtn.setOnClickListener(this);
        subMitBtn.setOnClickListener(this);
        createInventoryGoodView = view.findViewById(R.id.createInventoryGoodView);
        createInventoryGoodView.setCreateGoodFragment(this);
        unInventoryGoodView = view.findViewById(R.id.unInventoryGoodView);
        unInventoryGoodView.setCreateGoodFragment(this);
        mRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {

            if (checkedId == R.id.inventoryGood) {
                mIsInventoryGood = true;
                createInventoryGoodView.setVisibility(View.VISIBLE);
                unInventoryGoodView.setVisibility(View.GONE);
            } else if (checkedId==R.id.unInventoryGood){
                mIsInventoryGood = false;
                unInventoryGoodView.setVisibility(View.VISIBLE);
                createInventoryGoodView.setVisibility(View.GONE);
            }
        });

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.cancleBtn) {
            ViewUtil.finishFragment(getActivity(), this);
        } else if (id == R.id.subMitBtn) {
            mGoodName = goodNameTv.getText().toString();
            if (TextUtils.isEmpty(mGoodName)) {
                ToastUtil.showToast("商品名字不能为空");
                return;
            }
            subMit(mGoodName);
        }

    }

    public void subMit(String goodName) {
        CreateGoodReq createGoodReq = null;
        if (mIsInventoryGood) {
            if (createInventoryGoodView.checkData(goodName)) {
                createGoodReq = createInventoryGoodView.getCreateGoodReq(goodName);
            }
        } else {
            String errorMsg = unInventoryGoodView.checkData(goodName);
            if (!TextUtils.isEmpty(errorMsg)) {
                ToastUtil.showToast(errorMsg);
                return;
            }
            createGoodReq = unInventoryGoodView.getCreateGoodReq(goodName);
        }
        if (createGoodReq != null) {

            if (goodDetailBean!=null){
                createGoodReq.setCode(goodDetailBean.getCode());
                createGoodReq.setVersion(goodDetailBean.getVersion());
                GoodModel.updateGood(this,goodDetailBean.getId(), createGoodReq, new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        ToastUtil.showToast("修改商品成功");
                        EvenBusUtil.updateOneKindGoodList();
                        ViewUtil.finishFragment(getActivity(), CreateGoodFragment.this);
                    }
                });
            }
            else {
                GoodModel.createGood(this, createGoodReq, new BaseHttpObserver<BaseResp>() {
                    @Override
                    protected void handleResultOkResp(BaseResp resp) {
                        ToastUtil.showToast("新增商品成功");
                        EvenBusUtil.updateAllGoodList();
                        ViewUtil.finishFragment(getActivity(), CreateGoodFragment.this);
                    }
                });
            }

        }

    }

    public void showKind(CustomItemChosenButton chosenButton) {
        KeyboardUtils.hideKeyboard(getActivity());
        if (kindBeans.isEmpty()) {
            getKinds(true, chosenButton);
        } else {
            chosenButton.setCustomItemList(kindBeans, false);
            chosenButton.show();
        }
    }

    public void getKinds(boolean isShowView, CustomItemChosenButton chosenButton) {

        if (isShowView) {
            LoadingDialog.newLoadingDialog().show(getActivity());
        }
        GoodModel.getCategory(null, 0, new BaseHttpObserver<BaseListResp<KindBean>>() {
            @Override
            protected void handleResultOkResp(BaseListResp<KindBean> resp) {
                kindBeans = resp.getDatas();
                if (isShowView) {
                    chosenButton.setCustomItemList(resp.getDatas());
                    chosenButton.show();
                }

            }
        });
    }

    public void choiceUnit(CustomItemChosenButton chosenButton,boolean mIsInventoryUnit) {
        KeyboardUtils.hideKeyboard(getActivity());
        if (unitBeans == null) {
            getUnits(chosenButton,mIsInventoryUnit);
        } else {
            if (!mIsInventoryUnit){
                if (unitBeans.get(0).getId()>0){
                    UnitBean unitBean = new UnitBean();
                    unitBean.setName("清除已选");
                    unitBean.setId(-1);
                    unitBeans.add(0, unitBean);
                }
            }
            else {
                if (unitBeans.get(0).getId()<0) {
                    unitBeans.remove(0) ;
                }
            }
            if (unitBeans.size() > 20) {
                chosenButton.setNum(4);
            }
            chosenButton.setCustomItemList(unitBeans, false);
            chosenButton.show();
        }
    }

    public void getUnits(CustomItemChosenButton chosenButton,boolean mIsInventoryUnit) {

        UnitModel.getUnits(null, new BaseHttpObserver<BasePageData<UnitBean>>() {
            @Override
            protected void handleResultOkResp(BasePageData<UnitBean> resp) {
                if (resp != null && resp.getData() != null) {
                    unitBeans = resp.getData().getDatas();
                    if (!mIsInventoryUnit){
                        UnitBean unitBean = new UnitBean();
                        unitBean.setName("清空");
                        unitBean.setId(-1);
                        unitBeans.add(0, unitBean);
                    }
                    if (chosenButton != null) {
                        chosenButton.setCustomItemList(unitBeans);
                        chosenButton.show();
                    }
                }

            }
        });
    }

    public void createKind(CustomItemChosenButton chosenButton) {

        DialogUtil.showInputDialog(getActivity(), "新增分类", "", new InputDialog.OnSubMitBtnClickListener() {
            @Override
            public void onSubMitBtnClick(String inputData, InputDialog inputDialog) {
                if (TextUtils.isEmpty(inputData)) {
                    ToastUtil.showToast("分类名字不能为空");
                    return;
                }
                ArrayMap<String, Object> kindData = new ArrayMap<>();
                kindData.put("name", inputData);
                KindModel.createKind((IBaseView) getActivity(), kindData, new BaseHttpObserver<DataResp<Long>>() {
                    @Override
                    protected void handleResultOkResp(DataResp<Long> resp) {
                        ToastUtil.showToast("新增分类成功");
                        KindBean kindBean = new KindBean();
                        kindBean.setId(resp.getData());
                        kindBean.setName(inputData);
                        mChoiceKind = kindBean;
                        chosenButton.setText(inputData);
                        inputDialog.dismiss();
                        getKinds(false, null);

                    }
                });
            }
        });
    }



}
